package com.xmu.edu.tisp.contract.landimplementation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.xmu.edu.tisp.R;
import com.xmu.edu.tisp.service.contract.XJSZ_ZYSQQR_DETAIL_service;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ImplementationListAdapter extends BaseAdapter {
	private Context context;
	private List<HashMap<String, String>> list;
	HashMap<String, String> editHash = new HashMap<String, String>();

	private String FARMER_CD;

	public ImplementationListAdapter(Context context,
			List<HashMap<String, String>> list, String FARMER_CD) {
		super();
		this.context = context;
		this.list = list;
		this.FARMER_CD = FARMER_CD;
		Iterator it = list.iterator();
		int index = 0;
		while (it.hasNext()) {
			editHash.put(list.get(index).get("XJSZ_ZYSQQR_DETAIL_ID"),
					list.get(index).get("ZS"));
			index++;
			it.next();
		}
		/*xjsz_detail_zysqqr = new XJSZ_ZYSQQR_DETAIL_service(context);*/
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int index = position;
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.implementationlistview_item, null);

		}
		HashMap<String, String> map = list.get(position);
		LinearLayout linear1 =  (LinearLayout) view.findViewById(R.id.ILV_item_linearlayout1);
		TextView field_cd = (TextView) view.findViewById(R.id.field_cd);
		field_cd.setText(map.get("FIELD_BLOCK_CODE"));
		linear1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*ArrayList<String> ZS = new ArrayList<String>();*/
                /*LinearLayout temp_linear = (LinearLayout)v.findViewById(R.id.ILV_item_linearlayout1);
                temp_linear.setBackgroundResource(R.drawable.listviewitemselector);*/
				/*Iterator it = list.iterator();
				int temp = 0;
				while (it.hasNext()) {

					System.out.println(list.get(temp).get("FIELD_BLOCK_CODE"));
					ZS.add(editHash
							.get(list.get(temp).get("FIELD_BLOCK_CODE")));
					System.out.println(editHash.get(list.get(temp).get(
							"FIELD_BLOCK_CODE")));
					temp++;
					it.next();
				}*/
				SaveData();

				Intent intent = new Intent();
				intent.putExtra("XJSZ_ZYSQQR_ID", list.get(index).get("XJSZ_ZYSQQR_ID"));
				intent.putExtra("XJSZ_ZYSQQR_DETAIL_ID",
						list.get(index).get("XJSZ_ZYSQQR_DETAIL_ID"));
				intent.putExtra("FARMER_CD", FARMER_CD);
				intent.setClass(context, Modify_LandImplement.class);
				context.startActivity(intent);
			}

		});
		String XJSZ_D_ID = map.get("XJSZ_ZYSQQR_DETAIL_ID");

		TextView field_name = (TextView) view.findViewById(R.id.field_name);
		field_name.setText("(" + map.get("FIELD_MANAGER") + ")");

		// EditText zs_editText = ( EditText )
		// view.findViewById(R.id.zs_edittext);

		EditText edit = (EditText) view.findViewById(R.id.zs_edittext);
		edit.setOnFocusChangeListener(new OnFoucus(XJSZ_D_ID));
		//edit.addTextChangedListener(new TextChanged(XJSZ_D_ID));
		
		if (editHash.containsKey(XJSZ_D_ID)) {
			
			edit.setText(editHash.get(XJSZ_D_ID));
		}

		else {
			edit.setText("");
		}

		return view;
	}

	public void getEditHash() {
		SaveData();

	}
	
	class TextChanged implements TextWatcher{
		String XJSZ_D_ID;
		
		public TextChanged (String XJSZ_D_ID){
			this.XJSZ_D_ID = XJSZ_D_ID;
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			String etText = s.toString();
			System.out.println(XJSZ_D_ID);
			System.out.println(etText);
			editHash.put(XJSZ_D_ID, etText);
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			
		}
		
	}

	class OnFoucus implements OnFocusChangeListener {

		String XJSZ_D_ID;

		public OnFoucus(String XJSZ_D_ID) {
			this.XJSZ_D_ID = XJSZ_D_ID;
		}

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			EditText mEt = (EditText) v;
			String etText = mEt.getText().toString();
			System.out.println(XJSZ_D_ID);
			System.out.println(etText);
			editHash.put(XJSZ_D_ID, etText);

		}

	}

	private void SaveData() {

/*		System.out.println("111111111111111");
		Iterator it = ZS.iterator();
		int index = 0;
		while (it.hasNext()) {
			String xjsz_zysqqr_detail_id = list.get(index).get(
					"XJSZ_ZYSQQR_DETAIL_ID");
			Cursor cur = xjsz_detail_zysqqr
					.fetchDataByXJSZ_ZYSQQR_DETAIL_ID(xjsz_zysqqr_detail_id);
			String xh = null;

			String field_block_id = null;
			String pzdm = null;
			String zs = (String) it.next();
			while (cur.moveToNext()) {
				xh = cur.getString(cur.getColumnIndex("XH"));

				field_block_id = cur.getString(cur
						.getColumnIndex("FIELD_BLOCK_ID"));
				pzdm = cur.getString(cur.getColumnIndex("PZDM"));
			}
			cur.close();
			System.out.println(xjsz_zysqqr_detail_id);

			
			index++;

		}*/
		SharedPreferences sp = context.getSharedPreferences(FARMER_CD+"LI", Activity.MODE_APPEND);
		SharedPreferences.Editor editor=sp.edit();
		int count = sp.getInt("FIELD_BLOCK_NUMBER", 0);
		for(int i = 0;i<count ; ++i){
			if(sp.getString("XJSZ_ZYSQQR_DETAIL_ID"+i, "").equals("")){
				continue;
			}
			else{
				System.out.println(Integer.valueOf(editHash.get(sp.getString("XJSZ_ZYSQQR_DETAIL_ID"+i, ""))));
				editor.putInt("ZS"+i, Integer.valueOf(editHash.get(sp.getString("XJSZ_ZYSQQR_DETAIL_ID"+i, ""))));
				System.out.println(editHash.get(sp.getString("XJSZ_ZYSQQR_DETAIL_ID"+i, "")));
			}
		}
		editor.commit();
		
		
		
		
	}

	




}
